// @charset "utf-8";

define(function(require, exports, module) {
	var publicMethod = require('../../public/public'),
		clientApi = require('../../public/clientapi');

	var template = require('../template/template');

	var initRobot = {
		init: function () {
			var self = this;
			$('#chat-input').on('keydown', function (e) {
				e = e || window.event;
				// var isAltKey = e.altKey && e.keyCode == 83;
				if (e.keyCode == 13) {
					e.preventDefault();
					self.checkText();
				}
			})
			$('#submit-btn').on('click', function () {
				self.checkText();
			});
		},
		checkText: function () {
			var self = this;
			var trimTxt = $.trim($('#chat-input').val());
			if (trimTxt == $('#chat-input').attr('data-val') || trimTxt === '') {
				return;
			}

			//判断系统模块



			self.sendMessage(trimTxt);
		},
		sendMessage: function (qTxt) {
			var self = this;
			qTxt = qTxt || $.trim($('#chat-input').val());
			template.renderUserMsg(qTxt);


			self.requestAnswer(qTxt);
			// publicMethod.chatScrollTo();
			$('#chat-input').val('');
		},
		requestAnswer: function (qTxt) {
			var self = this;
			$.support.cors = true;
			var params = {
				username: clientApi.userName,
				userid: clientApi.userId,
				question: qTxt
			};
			var url = 'http://sp.10jqka.com.cn/liuyan/message/getroutereply/';
			publicMethod.ajaxJson({
				type: 'POST',
				url: url,
				data: params,
				callback: function(result) {
					alert(JSON.stringify(result));
				}

			});
		}
	};

	module.exports = initRobot;
});